import axios from 'axios'
import qs from 'qs'

/**
 * Created by 白雨浓 on 17-8-10 上午11:29.
 *
 * ajax axios 封装
 */

const SUCCESS_CODE = 1;

if (process.env.NODE_ENV === 'development')
  axios.defaults.baseURL = "/api";
else
  axios.defaults.baseURL = "http://localhost:3000";

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

const Ajax = {
  errHandler(Vue, err) {
    /* if (err.response !== undefined)
      Vue.prototype.$message.error(`请求错误： ${JSON.stringify(err.response.status)}`);
    else
      Vue.prototype.$message.error(err); */
  },
  install(Vue) {
    const $$get = (path, params) => {

      if (params !== undefined) {
        let b = true;
        for (let key of params) {
          if (b) {
            path += `?${key}=${params[key]}`;
            b = !b;
          } else
            path += `&${key}=${params[key]}`
        }
      }

      return new Promise((resolve, reject) => {
        axios.get(path).then(res => {
          if (res.data.code < SUCCESS_CODE)
            throw res.data.msg;

          resolve([res.data, res]);
        }).catch((err) => {
          this.errHandler(Vue, err);
          reject(err);
        })
      })
    };

    const $$post = (path, params) => {
      return new Promise((resolve, reject) => {
        axios.post(path, qs.stringify(params))
          .then(res => {
            if (res.data.code < SUCCESS_CODE)
              throw res.data.msg;

            resolve([res.data, res]);
          })
          .catch((err) => {
            this.errHandler(Vue, err);
            reject(err);
          })
      })
    };

    const $$put = (path, params) => {
      return new Promise((resolve, reject) => {
        axios.put(path, qs.stringify(params))
          .then(res => {
            if (res.data.code < SUCCESS_CODE)
              throw res.data.msg;

            resolve([res.data, res]);
          })
          .catch((err) => {
            this.errHandler(Vue, err);
            reject(err);
          })
      })
    };

    const $$patch = (path, params) => {
      return new Promise((resolve, reject) => {
        axios.patch(path, qs.stringify(params))
          .then(res => {
            if (res.data.code < SUCCESS_CODE)
              throw res.data.msg;

            resolve([res.data, res]);
          })
          .catch((err) => {
            this.errHandler(Vue, err);
            reject(err);
          })
      })
    };

    const $$delete = (path, params) => {
      if (params !== undefined) {
        let b = true;
        for (let key of params) {
          if (b) {
            path += `?${key}=${params[key]}`;
            b = !b;
          } else
            path += `&${key}=${params[key]}`
        }
      }

      return new Promise((resolve, reject) => {
        axios.delete(path).then(res => {
          if (res.data.code < SUCCESS_CODE)
            throw res.data.msg;

          resolve([res.data, res]);
        }).catch((err) => {
          this.errHandler(Vue, err);
          reject(err);
        })
      })
    };


    const $$postj = (path, json) => {
      return new Promise((resolve, reject) => {
        axios.post(path, json, {
            headers: {
              "Content-Type": "application/json"
            }
          })
          .then(res => {
            if (res.data.code < SUCCESS_CODE)
              throw res.data.msg;

            resolve([res.data, res]);
          })
          .catch((err) => {
            this.errHandler(Vue, err);
            reject(err);
          })
      })
    };

    const $$putj = (path, params) => {
      return new Promise((resolve, json) => {
        axios.put(path, json, {
            headers: {
              "Content-Type": "application/json"
            }
          })
          .then(res => {
            if (res.data.code < SUCCESS_CODE)
              throw res.data.msg;

            resolve([res.data, res]);
          })
          .catch((err) => {
            this.errHandler(Vue, err);
            reject(err);
          })
      })
    };

    const $$patchj = (path, json) => {
      return new Promise((resolve, reject) => {
        axios.patch(path, json, {
            headers: {
              "Content-Type": "application/json"
            }
          })
          .then(res => {
            if (res.data.code < SUCCESS_CODE)
              throw res.data.msg;

            resolve([res.data, res]);
          })
          .catch((err) => {
            this.errHandler(Vue, err);
            reject(err);
          })
      })
    };

    Vue.prototype.$$ajax = axios;
    Vue.prototype.$$get = $$get;
    Vue.prototype.$$post = $$post;
    Vue.prototype.$$put = $$put;
    Vue.prototype.$$patch = $$patch;
    Vue.prototype.$$delete = $$delete;
    Vue.prototype.$$postj = $$postj;
    Vue.prototype.$$putj = $$putj;
    Vue.prototype.$$patchj = $$patchj;
  }
};


export default Ajax;